﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using EnvironmentalMonitor.Support.Hibernate.Manage;
using EnvironmentalMonitor.Support.Hibernate;
using EnvironmentalMonitor.Support.Module.Manage;
using EnvironmentalMonitor.Support.Module;

namespace EnvironmentalMonitor.Support.Business.Manage
{
    /// <summary>
    /// 菜单业务类
    /// </summary>
    public class MenuBusiness
    {
        /// <summary>
        /// 分页查询
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="rows">每页行数</param>
        /// <param name="total">总数</param>
        /// <returns>菜单集合</returns>
        public List<Menu> Query(int page, int rows, ref int total)
        {
            List<Menu> results = new List<Menu>();

            MenuHibernate hibernate = new MenuHibernate();
            results = hibernate.Query(page, rows, ref total);

            return results;
        }

        /// <summary>
        /// 增加
        /// </summary>
        /// <param name="value">值</param>
        /// <returns>结果</returns>
        public bool Insert(Menu value)
        {
            bool result = false;

            MenuHibernate hibernate = new MenuHibernate();
            result = hibernate.Insert(value);

            return result;
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="value">值</param>
        /// <returns>结果</returns>
        public bool Update(Menu value)
        {
            bool result = false;

            MenuHibernate hibernate = new MenuHibernate();
            result = hibernate.Update(value);

            return result;
        }

        /// <summary>
        /// 同步
        /// </summary>
        /// <param name="values">所有菜单</param>
        /// <param name="user">操作用户</param>
        /// <returns>结果</returns>
        public bool Sync(List<Menu> values, User user)
        {
            bool result = true;

            int total = 0;
            List<Menu> hasMenus = this.Query(1, int.MaxValue, ref total);

            List<Menu> deleteMenus = new List<Menu>();
            List<Menu> addMenus = new List<Menu>();

            bool has = false;
            for (int i = 0; i < hasMenus.Count; i++)
            {
                has = false;
                for (int j = 0; j < values.Count; j++)
                {
                    if (hasMenus[i].Url == values[j].Url)
                    {
                        has = true;
                        break;
                    }
                }
                if (!has)
                {
                    deleteMenus.Add(hasMenus[i]);
                }
            }

            for (int i = 0; i < values.Count; i++)
            {
                has = false;
                for (int j = 0; j < hasMenus.Count; j++)
                {
                    if (values[i].Url == hasMenus[j].Url)
                    {
                        has = true;
                        break;
                    }
                }
                if (!has)
                {
                    addMenus.Add(values[i]);
                }
            }

            MenuHibernate menuHibernate = new MenuHibernate();
            PostMenuHibernate postMenuHibernate = new PostMenuHibernate();

            // 删除
            if (result)
            {
                for (int i = 0; i < deleteMenus.Count; i++)
                {
                    result = postMenuHibernate.DeleteByMenu(deleteMenus[i].Guid);
                    if (!result)
                    {
                        break;
                    }
                    result = menuHibernate.Delete(deleteMenus[i].Guid);
                    if (!result)
                    {
                        break;
                    }
                }
            }

            // 增加
            if (result)
            {
                for (int i = 0; i < addMenus.Count; i++)
                {
                    DateTime now = DateTime.Now;

                    Menu menu = new Menu();
                    menu.InsertUserId = user.Guid;
                    menu.InsertTime = now;
                    menu.UpdateUserId = user.Guid;
                    menu.UpdateTime = now;
                    menu.Remark = string.Empty;
                    menu.Validity = true;

                    menu.CatalogId = string.Empty;
                    menu.Name = addMenus[i].Name;
                    menu.Serial = int.MaxValue;
                    menu.Url = addMenus[i].Url;
                    menu.Parameter = string.Empty;

                    result = menuHibernate.Insert(menu);
                    if (!result)
                    {
                        break;
                    }
                }
            }

            return result;
        }
    }
}
